/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.admin.model;

import com.entities.EventInfo;
import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

/**
 *
 * @author Tuan Long
 */
@Stateless
public class EventInfoFacade extends AbstractFacade<EventInfo> implements EventInfoFacadeLocal {
    @PersistenceContext(unitName = "BluePumpkinEJBPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public EventInfoFacade() {
        super(EventInfo.class);
    }

    @Override
    public boolean createNewEvent(String name, String des, String type, Date timeS, Date timeE, String avatar, String shortdes,String address) {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventInfo.createNewEvent");
        query.setParameter("name", name);
        query.setParameter("des", des);
        query.setParameter("type", type);
        query.setParameter("timeS", timeS);
        query.setParameter("timeE", timeE);
        query.setParameter("avatar", avatar);
        query.setParameter("shortdes", shortdes);
        query.setParameter("address", address);
        int results = query.executeUpdate();
        return results > 0;        
    }

    @Override
    public EventInfo getEventInfoByEventId(int eventId) {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventInfo.getEventInfoContentById");
        query.setParameter("eventId", eventId);
        Object[] row = (Object[]) query.getSingleResult();
        String avatar = (String) row[0];
        String shortdes = (String) row[1];
        Date created = (Date) row[2];
        String address = (String) row[3];
        EventInfo ei = new EventInfo();
        ei.setAvatarImg(avatar);
        ei.setEventShortDesc(shortdes);
        ei.setCreated(created);
        ei.setAddress(address);
        return ei;
    }

    @Override
    public boolean updateEvent(int eventId, String name, String des, String type, Date start, Date end, String avatar, String shortdes, String address) {
        StoredProcedureQuery query = em.createNamedStoredProcedureQuery("EventInfo.updateEventByEventId");
        query.setParameter("eventId", eventId);
        query.setParameter("name", name);
        query.setParameter("des", des);
        query.setParameter("type", type);
        query.setParameter("timeS", start);
        query.setParameter("timeE", end);
        query.setParameter("avatar", avatar);
        query.setParameter("shortdes", shortdes);
        query.setParameter("address", address);
        int result = query.executeUpdate();
        return result >0;
    }
    
}
